import java.awt.*;
import java.awt.geom.Point2D;

// 胚胎类
class Embryo {
    private static final int[] SIZES = {30, 35, 40, 45, 50};
    private static final Color[] COLORS = {
            new Color(255, 192, 203), new Color(255, 182, 193),
            new Color(255, 172, 183), new Color(255, 162, 173),
            new Color(255, 152, 163)
    };

    protected static final float MAX_SPEED = 5.0f;
    private float x, y;
    private float vx, vy;
    private float folicAcid = 50;
    private float iron = 50;
    protected int size;
    private Color color;
    private static final float SPEED = 4.0f;
    private float speedModifier = 1.0f;

    public Embryo(float x, float y) {
        this.x = x;
        this.y = y;
        updateStage(0);
    }

    public void update(boolean[] directions) {
        // 营养物质随时间衰减（每秒减1）
        folicAcid = Math.max(0, folicAcid - 1 / 60f);
        iron = Math.max(0, iron - 1 / 60f);

        // 铁含量影响移动速度
        float finalSpeed = SPEED * (iron < 20 ? 0.5f : 1.0f) * speedModifier;

        // 移动逻辑
        vx = 0;
        vy = 0;
        if (directions[0]) vy -= finalSpeed;
        if (directions[1]) vy += finalSpeed;
        if (directions[2]) vx -= finalSpeed;
        if (directions[3]) vx += finalSpeed;

        // 边界限制
        x = Math.max(size / 2f, Math.min(Main.WIDTH - size / 2f, x + vx));
        y = Math.max(size / 2f, Math.min(Main.HEIGHT - size / 2f, y + vy));
    }

    public void updateStage(int stage) {
        size = SIZES[stage];
        color = COLORS[stage];
    }


    public boolean collidesWith(GameElement element) {
        float dx = x - element.x;
        float dy = y - element.y;
        float minDist = (size / 2f + element.size / 2f);
        return dx * dx + dy * dy < minDist * minDist;
    }

    public void draw(Graphics2D g) {
        long time = System.currentTimeMillis();

        // 轻微浮动效果
        float floatOffset = (float) Math.sin(time * 0.002) * 3f;

        RadialGradientPaint rgp = new RadialGradientPaint(
                new Point2D.Float(x, y + floatOffset),
                size * 1.2f,
                new float[]{0f, 0.8f, 1f},
                new Color[]{new Color(255, 255, 255, 150),
                        new Color(color.getRed(), color.getGreen(), color.getBlue(), 200),
                        color}
        );

        g.setPaint(rgp);
        g.fillOval((int) (x - (float) size / 2), (int) (y - (float) size / 2 + floatOffset), size, size);

        int pulse = (int) (2 * Math.sin(time * 0.005));
        g.setColor(new Color(255, 255, 255, 120));
        int nucleusSize = size / 3 + pulse;
        g.fillOval(
                (int) (x - (float) nucleusSize / 2),
                (int) (y - (float) nucleusSize / 2 + floatOffset),
                nucleusSize, nucleusSize
        );


    }

    public void applySpeedModifier(float modifier, float duration) {
        this.speedModifier = modifier;
        // 使用线程安全的Timer
        new java.util.Timer().schedule(
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        speedModifier = 1.0f;
                    }
                },
                (long) (duration * 1000) // 转换为毫秒
        );
    }

    // 增加叶酸值
    public void addFolicAcid(float amount) {
        folicAcid = Math.min(100, folicAcid + amount);
    }

    // 增加铁含量
    public void addIron(float amount) {
        iron = Math.min(100, iron + amount);
    }

    // 获取蛋白质效果系数（叶酸<20时返回0.5）
    public float getProteinEffectModifier() {
        return (folicAcid < 20) ? 0.5f : 1.0f;
    }

    // 新增Getter方法
    public float getFolicAcid() {
        return folicAcid;
    }

    public float getIron() {
        return iron;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public float getSpeed() {
        return (float) Math.hypot(vx, vy);
    }

    public float getSize() {return size;}
}
